package com.tencent.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tencent.entity.Checkitem;
import com.tencent.entity.PageResult;
import com.tencent.entity.Pagination;
import com.tencent.entity.QueryPageBean;
import com.tencent.mapper.CheckItemMapper;
import com.tencent.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author: aiming    @date: 2022/8/13 13:48
 * @description: 检查项服务
 */
@Service
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
    //注入mapper对象
    @Autowired
    private CheckItemMapper checkItemMapper;
    public void add(Checkitem checkitem) {
        checkItemMapper.add(checkitem);
    }

    //检查项分页查询
    public PageInfo<Checkitem> pageQuery(Pagination pagination) {
        System.out.println(pagination);
        PageHelper.startPage(pagination.getCurrentPage(), pagination.getPageSize());
        List<Checkitem> checkitems = checkItemMapper.selectByCondition(pagination.getQueryString());
        return new PageInfo<>(checkitems);

//        Integer currentPage = queryPageBean.getCurrentPage();
//        Integer pageSize = queryPageBean.getPageSize();
//        String queryString = queryPageBean.getQueryString();
//
//        //mybatis框架提供的分页助手进行分页查询
//        PageHelper.startPage(currentPage, pageSize);
//
//        Page<Checkitem> page = checkItemMapper.selectByCondition(queryString);
//        long total = page.getTotal();
//        List<Checkitem> rows = page.getResult();
//        return new PageResult(total,rows);
    }

    //根据ID删除检查项
    public void deleteById(Integer id) {
        //判断当前检查项是否已经关联检查组
        Long count = checkItemMapper.findCountByCheckItemId(id);
        if (count > 0){
            //当前检查项已经被关联到检查组，不允许删除
            new RuntimeException();
        }
        checkItemMapper.deleteById(id);
    }

    public void edit(Checkitem checkitem){
        checkItemMapper.edit(checkitem);
    }

    @Override
    public Checkitem findById(Integer id) {
        return checkItemMapper.findById(id);
    }
}
